import { ref, reactive } from 'vue'
import { EditType, Title } from '@/type/BaseEnum';
import { hazardDetailApi ,hazardUpdateApi} from '@/api/ReviewBusiness/01Project/project'
import useInstance from '@/hooks/useInstance';

export default function dispatchModel(getTableList?) {
  const {global} = useInstance()
  const RefDispatchList = ref<{ show: (type: string,action:number, row?: any) => void }>()
  let dialogModel = reactive<any>({
    id: '',
    recordId: '',
    fileId: '',
    pageNumber: '',
    hazardLevel: '',
    description: '',
    causeAnalysis: '',
    treatment: '',
    status: '',
    createId: '',
    updateId: '',
    hazardCites: [] as any,
    hazardLogs: [] as any,
    createTime: '',
    updateTime: '',
  })
  const rules = reactive({
    updateId: [{ required: true, message: '请选择指派人', trigger: 'change' }]
  })
  const assignDiapatchBtn = async (row: any,action) => {
    const res = await hazardDetailApi({ id: row.id })
    RefDispatchList.value?.show(EditType.EDIT,action, res.data)
  }
  const saveDispatch =async (form,actionValue) => {
    // 更新的接口
    form.action=actionValue
    const res = await hazardUpdateApi(form)
    if(res.code === 200){
      const msg = actionValue===1?'指派成功':actionValue===2?'异常单已完成':actionValue===3?'异常单已终止':''
      global.$message({type:'success',message:`${msg}`})
      getTableList()
    }
  }
  return {
    RefDispatchList, assignDiapatchBtn, dialogModel, rules, saveDispatch
  }
}